<?php

/**
 * Created by PhpStorm.
 * User: Tommy
 * Date: 2018/11/9
 * Time: 16:18
 */

namespace App\Controllers;


use App\Services\HallManageService;
use App\Services\HallService;
use common\enum\CommonEnum;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use common\utils\SysCode;
use App\Models\HallDomain;
use App\Models\Hall;

class HallController extends BaseController
{

    /**
     * 厅主名称列表
     * @return mixed
     */
    public function hallNameList()
    {
        return HallService::hallNameList();
    }


    /**
     * 获取厅主列表
     * @param Request $request
     * @return array
     * @throws \Illuminate\Validation\ValidationException
     */
    public function getList(Request $request)
    {
        $this->validate($request, [
            'hall_id' => 'integer',
            'status' => [Rule::in([CommonEnum::DISABLE, CommonEnum::ENABLED, CommonEnum::DELETE])],
            'page' => 'integer',
            'page_size' => 'integer',
        ]);
        $value = $request->input();
        return HallManageService::hallList($value);
    }

    /**
     * 模糊搜索
     * @param Request $request
     * @return mixed
     * @throws \Illuminate\Validation\ValidationException
     */
    public function search(Request $request)
    {
        $this->validate($request, [
            'hall_name' => 'required|max:255',
        ]);
        $hallName = $request->input('hall_name');
        return HallManageService::search($hallName);
    }

    /**
     * 修改google开关
     * @param Request $request
     * @return mixed
     * @throws \Illuminate\Validation\ValidationException
     */
    public function googleSwitch(Request $request)
    {
        $this->validate($request, [
            'hall_id' => 'required',
            'status' => 'required',
        ]);
        $value = $request->input();
        return HallManageService::updateGoogle($value);
    }

    /**
     * 修改运营时间
     * @param Request $request
     * @return mixed
     * @throws \Illuminate\Validation\ValidationException
     */
    public function startTime(Request $request)
    {
        $this->validate($request, [
            'hall_id' => 'required',
            'start_time' => 'required',
        ]);
        $value = $request->input();
        return HallManageService::startTime($value);
    }

    public function getEmergencyList()
    {
        return [
            'WhatsApp',
            'Skype',
            '手机号',
            'potato',
            'SOMA',
            '微信',
            'QQ',
        ];
    }

    public function addHall(Request $request)
    {
        $this->validate($request, [
            'company_name' => 'required',
            'admin_name' => 'required',
            'domains' => 'required',
            'emergency' => 'required',
            'emergency_code' => 'required',
            'is_use_googlekey' => 'required',
            'status' => 'required',
            'partner_list' => 'required',
        ]);

        $adminUsername = $request->input('admin_name');
        if (HallService::adminExist($adminUsername)) {
            return error_exit(SysCode::USER_EXIST, ['username' => $adminUsername]);
        }

        $companyName = $request->input('company_name');
        if (Hall::where(['company_name' => $companyName])->first()) {
            return error_exit(SysCode::HALL_NAME_EXIST, ['company_name' => $companyName]);
        }

        $domains = $request->input('domains');
        $list = HallDomain::whereIn('domain', $domains)->get()->toArray();
        if (!empty($list)) {
            $domains = array_column($list, 'domain');
            return error_exit(SysCode::HALL_DOMAIN_EXIST, ['domains' => implode(',', $domains)]);
        }

        $params = $request->all();
        return HallService::addHall($params);
    }

}